/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    dw_rtc.h
 *  @brief   Designware rtc driver header file
 *  @version v1.0
 *  @date    03. Apr. 2023
 *  @author  liuchao
 ****************************************************************/

#ifndef __DW_RTC_H__
#define __DW_RTC_H__

#include <stdint.h>
#include "dev_common.h"
#include "datetime.h"

#ifdef __cplusplus
extern "C" {
#endif

//enable the interrupt generation
#define RTC_IEN                         BIT(0)
//interrupt masked
#define RTC_MASK                        BIT(1)
//enable the counter
#define RTC_EN                          BIT(2)
//enable the WRAP
#define RTC_WEN                         BIT(3)
//enable the prescaler counter
#define RTC_PSCLR_EN                    BIT(4)

#define RTC_INT_ACTIVE                  BIT(0)

struct dw_rtc {
	/* current counter value */
	const volatile uint32_t RTC_CCVR;
	/* counter match */
	volatile uint32_t RTC_CMR;
	/* counter load */
	volatile uint32_t RTC_CLR;
	/* counter control */
	volatile uint32_t RTC_CCR;
	/* interrupt status */
	const volatile uint32_t RTC_STAT;
	/* interrupt raw status */
	const volatile uint32_t RTC_RSTAT;
	/* end of interrupt */
	const volatile uint32_t RTC_EOI;
	/* component version */
	const volatile uint32_t RTC_COMP_VERSION;
	/* counter prescaler */
	volatile uint32_t RTC_CPSR;
	/* current counter prescaler */
	const volatile uint32_t RTC_CPCVR;
};

void    rtc_init(void);
void    rtc_start(void);
void    rtc_stop(void);
void    rtc_isr(void *args);
int32_t rtc_get_time(Datetime_t_Ptr tm);
void rtc_set_time(Datetime_t_Ptr tm);
int32_t rtc_set_alarm(uint32_t seconds);
void    rtc_register_callback(DEV_CALLBACK callback);

#ifdef __cplusplus
}
#endif

#endif /* __DW_RTC_H__ */
